package View;

import Controller.CustomerController;
import pojo.Customer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

//顾客添加界面
public class Add_window extends Component {
    private final JFrame frame = new JFrame("添加窗口");
    private final JPanel addPanel = new JPanel(); // 面板

    private CustomerController customerController;

    {
        try {
            customerController = new CustomerController();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    private JButton editButton = new JButton("修改");
    private final JButton saveButton = new JButton("保存");
    private final JButton backButton = new JButton("返回");

    private final JLabel nameLabel = new JLabel("姓名:");
    private final JTextField nameTextField = new JTextField(20);
    private final JLabel passwordLabel = new JLabel("密码:");
    private final JTextField passwordTextField = new JTextField(20);
    private final JLabel phoneNumberLabel = new JLabel("电话号码:");
    private final JTextField phoneNumberTextField = new JTextField(20);
    private final JLabel addressLabel = new JLabel("地址:");
    private final JTextField addressTextField = new JTextField(20);
    private final JLabel licenseNumberLabel = new JLabel("驾驶证号:");
    private final JTextField licenseNumberTextField = new JTextField(20);
    private final JLabel balanceLabel = new JLabel("余额:");
    private final JTextField balanceTextField = new JTextField(20);
    private final JLabel rentalCountLabel = new JLabel("租车数量:");
    private final JTextField rentalCountTextField = new JTextField(20);

    public Add_window() {
        // 添加窗口关闭事件监听器
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 这里可以添加你的关闭逻辑
                frame.setVisible(false);
                try {
                    new Car_window();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });
        frameControl();
    }

    // 设置容器管理
    public void frameControl() {
        // 设置底层容器和布局管理器
        frame.setSize(400, 300);
        frame.setLocation(500, 300);
        frame.setResizable(false);

        // 设置信息面板
        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new GridLayout(7, 2, 0, 10));
        infoPanel.add(nameLabel);
        infoPanel.add(nameTextField);
        infoPanel.add(passwordLabel);
        infoPanel.add(passwordTextField);
        infoPanel.add(phoneNumberLabel);
        infoPanel.add(phoneNumberTextField);
        infoPanel.add(addressLabel);
        infoPanel.add(addressTextField);
        infoPanel.add(licenseNumberLabel);
        infoPanel.add(licenseNumberTextField);
        infoPanel.add(balanceLabel);
        infoPanel.add(balanceTextField);
        infoPanel.add(rentalCountLabel);
        infoPanel.add(rentalCountTextField);

        // 设置按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
//        buttonPanel.add(editButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(backButton);

        // 设置整体布局
        frame.setLayout(new BorderLayout());
        frame.add(infoPanel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        // 设置按钮事件处理
        setButtonActions();

        // 设置窗口可见性和关闭操作
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    }
    // 获取当前时间
    private String getCurrentTimestamp(){
        // 获取当前时间
        Date d = new Date();

        long time = d.getTime();

        // 2、格式化日期对象，和时间 毫秒值。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedTimestamp = dateFormat.format(time);

        return formattedTimestamp;
    }
    // 设置按钮事件处理
    public void setButtonActions() {
        // 返回保存修改按钮事件处理
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取修改的数据
                String username = nameTextField.getText();//姓名
                String password = passwordTextField.getText();//密码
                String phoneNumber = phoneNumberTextField.getText();//电话号码
                String address = addressTextField.getText();//地址
                String licenseNumber = licenseNumberTextField.getText();//驾驶证号码
                int driver_license_number = 0;
                String balance = balanceTextField.getText();//余额
                int left_money = 0;
                String rentalCount = rentalCountTextField.getText();//租车数量
                int rentNumber = 0;

                //转换int
                try {
                    driver_license_number = Integer.parseInt(licenseNumber); // 尝试将驾驶证号码字符串转换为整数
                    left_money = Integer.parseInt(balance); // 尝试将余额字符串转换为整数
                    rentNumber = Integer.parseInt(rentalCount); // 尝试将租车数量字符串转换为整数
                } catch (Exception ex) {
                    // 处理转换失败的情况，比如给出错误消息或者设置默认值
                    System.err.println("前端转换类型错误: " + balance);
                    // 设置默认值
                    driver_license_number = 0;
                    left_money = 0; // 默认值
                    rentNumber = 0; // 默认值
                }

                // 保存修改的逻辑
                // 更新用户信息到数据库
                Customer customer = new Customer();
                customer.setName(username);
                customer.setPassword(password);
                customer.setContact(phoneNumber);
                customer.setAddress(address);
                customer.setDriverLicenseNumber(driver_license_number);
                customer.setLeftMoney(left_money);
                customer.setRentCarNumber(rentNumber);
//                String registrationDate = getCurrentTimestamp();
//                customer.setCustomer_id(AppData.getUserId());
                Date d = new Date();
                customer.setRegistrationDate(d);
                Boolean flag = null;
                try {
                    flag = customerController.addcustomer(customer);
                    System.out.println("flag:"+flag);
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (flag){
                    //保存成功的信息
                    JOptionPane.showMessageDialog(Add_window.this, "保存成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    // 返回主窗口
                    frame.setVisible(false);
                    try {
                        new Car_window();
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                }else {
                    //保存失败的信息
                    JOptionPane.showMessageDialog(Add_window.this, "保存失败！", "失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 返回主窗口
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 返回主窗口
                frame.setVisible(false);
                try {
                    new Car_window();
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                Add_window addWindow = new Add_window();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}
